package jt.classic.app.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.limp.mine.HTMLTool;

/**
 * EXT 树节点帮助类
 * @author lvt
 *
 */
public class Recursion {

	List<TreeNode> nodeList = new ArrayList<TreeNode>();

	StringBuffer returnStr = new StringBuffer();

	public List<TreeNode> getNodeList() {
		return nodeList;
	}

	public void setNodeList(List<TreeNode> nodeList) {
		this.nodeList = nodeList;
	}

	public StringBuffer getReturnStr() {
		return returnStr;
	}

	public void setReturnStr(StringBuffer returnStr) {
		this.returnStr = returnStr;
	}

	public void recursionFn(List list, TreeNode node) {
		List childList = getChildList(list, node);
		if (childList.size() > 0) {
			if (node.isZeroNode()) {

			} else {
				returnStr.append("{id:");
				returnStr.append(node.getId());
				returnStr.append(",text:'");
				returnStr.append(HTMLTool.htmlEscape(node.getText()));
				returnStr.append("',parentId:'");
				returnStr.append(node.getParentId());
				returnStr.append("',fullpathId:'");
				returnStr.append(node.getFullpathId());
				returnStr.append("',fullpath:'");
				returnStr.append(node.getFullpath());
				returnStr.append("',qtip:'");
				returnStr.append(node.getQtip());
				returnStr.append("',children:[");
			}

			Iterator it = childList.iterator();
			while (it.hasNext()) {
				TreeNode n = (TreeNode) it.next();
				recursionFn(list, n);
			}
			if (node.isZeroNode()) {

			} else {
				returnStr.append("]},");
			}
		} else {
			if(node.isZeroNode()){
				
			}else{
			returnStr.append("{id:");
			returnStr.append(node.getId());
			returnStr.append(",text:'");
			returnStr.append(HTMLTool.htmlEscape(node.getText()));
			returnStr.append("',parentId:'");
			returnStr.append(node.getParentId());
			returnStr.append("',fullpathId:'");
			returnStr.append(node.getFullpathId());
			returnStr.append("',fullpath:'");
			returnStr.append(node.getFullpath());
			returnStr.append("',qtip:'");
			returnStr.append(node.getQtip());
			returnStr.append("',leaf:"+node.isLeaf() +"},");
			}
		}

	}

	public List getChildList(List list, TreeNode node) { // 得到子节点列表
		List<TreeNode> li = new ArrayList<TreeNode>();
		synchronized (list) {
			Iterator it = list.iterator();
			while (it.hasNext()) {
				TreeNode n = (TreeNode) it.next();
				if (n.getParentId().equals(node.getId())) {
					li.add(n);
					it.remove(); // // list 作为全局变量， 这里remove所以要线程同步 ，
									// 如果不remove则不要线程同步也可。
				}
			}
		}
		return li;
	}

	public String modifyStr(String returnStr) {// 修饰一下才能满足Extjs的Json格式
		return ("[" + returnStr + "]").replaceAll(",]", "]");

	}

	public static void main(String[] args) {

		long _start = System.nanoTime();
		Recursion _r = new Recursion();
		_r.recursionFn(_r.nodeList, new TreeNode("1", "0", "1"));
		System.out.println(_r.modifyStr(_r.returnStr.toString()));
		long _end = System.nanoTime();
		System.out.println(_end - _start);
	}

}
